package io.gitee.kenewstar.dbmq.thread;

import io.gitee.kenewstar.dbmq.ConsumerTask;
import io.gitee.kenewstar.dbmq.data.DbDataHandler;

import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * @author kenewstar
 */
public class ClearThread extends CommonThread {

    private final DbDataHandler dbDataHandler;
    private final List<ConsumerTask> taskList;

    public ClearThread(DbDataHandler dbDataHandler, List<ConsumerTask> taskList) {
        this.dbDataHandler = dbDataHandler;
        this.taskList = taskList;
        this.setName("dbMq-clear-thread");
    }

    @Override
    public void run() {
        threadStatus = ThreadStatus.RUNNING;
        while (true) {
            try {
                if (threadStatus.equals(ThreadStatus.DEAD)) {
                    return;
                }
                this.dbDataHandler.reAssignMessage(taskList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.threadStatus = ThreadStatus.WAITING;
            LockSupport.park();
        }

    }

}
